package me.zhengjie.service.impl;

import me.zhengjie.dto.sqlServer.TaskResult;
import me.zhengjie.repository.TaskResultRepository;
import me.zhengjie.service.TaskResultServiceG;
import me.zhengjie.service.dto.TaskResultDto;
import me.zhengjie.service.dto.TaskResultQueryCriteria;
import me.zhengjie.service.mapper.TaskResultMapper;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
 * @website https://eladmin.vip
 * @description 服务实现
 * @author 1
 * @date 2025-06-16
 **/
@Service
@RequiredArgsConstructor
public class TaskResultServiceGImpl  implements TaskResultServiceG {
    private final TaskResultRepository taskResultRepository;
    private final TaskResultMapper taskResultMapper;

    @Override
    public PageResult<TaskResult> queryAll(TaskResultQueryCriteria criteria, Pageable pageable){
        Page<TaskResult> page = taskResultRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        PageResult<TaskResult> page1= PageUtil.toPage(page);
        return page1;
    }

    @Override
    public List<TaskResultDto> queryAll(TaskResultQueryCriteria criteria){
        return taskResultMapper.toDto(taskResultRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TaskResultDto findById(Integer id) {
        TaskResult taskResult = taskResultRepository.findById(id).orElseGet(TaskResult::new);
        ValidationUtil.isNull(taskResult.getId(),"TaskResult","id",id);
        return taskResultMapper.toDto(taskResult);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(TaskResult resources) {
        taskResultRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TaskResult resources) {
        TaskResult taskResult = taskResultRepository.findById(resources.getId()).orElseGet(TaskResult::new);
        ValidationUtil.isNull( taskResult.getId(),"TaskResult","id",resources.getId());
        taskResult.copy(resources);
        taskResultRepository.save(taskResult);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            taskResultRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TaskResultDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TaskResultDto taskResult : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("任务id", taskResult.getWmsTaskId());
            map.put("容器编码", taskResult.getLpnCode());
            map.put("实际取货地址", taskResult.getFromLocation());
            map.put("实际送货地址", taskResult.getToLocation());
            map.put("实际完成时间", taskResult.getCreated());
            map.put("任务状态", taskResult.getStatus());
            map.put("错误代码", taskResult.getIssueCode());
            map.put("异常原因", taskResult.getMessage());
            map.put("任务类型", taskResult.getWcsTaskType());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
